package com.abe.libquick.widget.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;


import com.abe.libquick.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


@SuppressLint("SimpleDateFormat")
public class DateTimePickerDialog2 extends Dialog {

    public static final int TYPE_DATE = 0;
    public static final int TYPE_TIME = 1;
    public static final int TYPE_DATE_TIME = 2;
    public int type = TYPE_DATE_TIME;
    protected Context mContext;
    private TextView title;
    private DatePicker datePicker;
    private TimePicker timePicker;
    private Button positiveButton;
    private Button negativeButton;
    private TextView et_riqi;
    private SimpleDateFormat format;
    // 获取一个日历对象，并初始化为当前选中的时间
    private Calendar mCalendar = Calendar.getInstance();

    public DateTimePickerDialog2(Context context) {
        super(context);
        this.mContext = context;
    }

    public DateTimePickerDialog2(Context context, TextView et_riqi) {
        this(context, et_riqi, et_riqi.getText().toString());
    }

    public DateTimePickerDialog2(Context context, TextView et_riqi, String date) {
        this(context, TYPE_DATE_TIME, et_riqi, date);
    }

    public DateTimePickerDialog2(Context context, int type, TextView et_riqi) {
        this(context, type, et_riqi, et_riqi.getText().toString());
    }

    public DateTimePickerDialog2(Context context, int type, TextView et_riqi,
                                 String date) {
        super(context);
        this.mContext = context;
        this.et_riqi = et_riqi;
        this.type = type;
        setTitle("请选择");
        switch (type) {
            case TYPE_DATE:
                format = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case TYPE_TIME:
                format = new SimpleDateFormat("HH:mm");
                break;
            default:
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                break;
        }
        try {
            mCalendar.setTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_datatimepicker);
        title = findViewById(R.id.item_name);
        datePicker = findViewById(R.id.dpPicker);
        timePicker = findViewById(R.id.tpPicker);
        title.setText("请选择");
        switch (type) {
            case TYPE_DATE:
                datePicker.setVisibility(View.VISIBLE);
                timePicker.setVisibility(View.GONE);
                break;
            case TYPE_TIME:
                datePicker.setVisibility(View.GONE);
                timePicker.setVisibility(View.VISIBLE);
                break;
            default:
                datePicker.setVisibility(View.VISIBLE);
                timePicker.setVisibility(View.VISIBLE);
                break;
        }
        positiveButton = findViewById(R.id.item_confirm);
        positiveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                et_riqi.setText(format.format(mCalendar.getTime()));
            }
        });
        negativeButton = findViewById(R.id.item_cancel);
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        datePicker.init(mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.DAY_OF_MONTH),
                new OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                        mCalendar.set(year, monthOfYear, dayOfMonth);
                    }
                });

        timePicker.setIs24HourView(true);
        timePicker
                .setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                    @Override
                    public void onTimeChanged(TimePicker view, int hourOfDay,
                                              int minute) {
                        mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        mCalendar.set(Calendar.MINUTE, minute);
                    }
                });

    }

    @Override
    public void show() {
        super.show();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.85); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }
}